package com.tensquare.gathering.service;

import com.tensquare.gathering.dao.GatheringDao;
import com.tensquare.gathering.pojo.Gathering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * & ProjectName：  tensquare-parent
 * & PackageName:   com.tensquare.gathering.service
 * & Creation_Date： 2020/3/31 02:44
 * & Author：しの
 * ----------------------------------
 * & Description:
 */
@Service
public class GatheringService {
    @Autowired
    private GatheringDao gatheringDao;

    //1.向缓存中放值
    //这里使用ognl表达式从参数取值作为key ，以方法返回值作为值放到springcache缓存中
    @Cacheable(key = "#id", value = "gathering")
    public Gathering findById(String id) {
        return gatheringDao.findById(id).get();
    }

    //清空缓存
    @CacheEvict(key = "#gathering.id", value = "gathering")
    public void update(Gathering gathering) {
        gatheringDao.save(gathering);
    }

    //清空缓存
    @CacheEvict(key = "#id", value = "gathering")
    public void deleteById(String id) {
        gatheringDao.deleteById(id);
    }
}
